﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Web.Mvc;
using twitterBootstrapAspNetMVCControls;

namespace System.Web.Mvc.Html
{
    public static class twitterBootstrapExtensions
    {
        public static string DateTimeFormate = "yyyy-mm-dd hh:ii";
        public static MvcHtmlString Button(this HtmlHelper helper,
                                               string innerHtml,
                                               object htmlAttributes)
        {
            return Button(helper, innerHtml,
                          HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)
            );
        }

        public static MvcHtmlString Button(this HtmlHelper helper,
                                           string innerHtml,
                                           IDictionary<string, object> htmlAttributes)
        {
            var builder = new TagBuilder("input");
            builder.InnerHtml = innerHtml;
            builder.MergeAttributes(htmlAttributes);
            return MvcHtmlString.Create(builder.ToString());
        }
        public static MvcHtmlString BootstrapBtn(this HtmlHelper htmlHelper, BootStrapBtn btn, object htmlAttributes)
        {

            return Html.LinkExtensions.ActionLink(htmlHelper, btn.LinkString, btn.ActionName, btn.ControllerName, null, htmlAttributes);
        }
        public static MvcHtmlString BootStrapBtnGroup(this HtmlHelper htmlHelper, BootStrapBtnGroup btnGroup, object htmlAttributes)
        {
            var h = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
            TagBuilder divPanel = new TagBuilder("div");
            divPanel.MergeAttributes(h);
            divPanel.Attributes.Add(new KeyValuePair<string, string>("class", "btn-group"));
            TagBuilder titleTag = new TagBuilder("button");
            KeyValuePair<string, string> @class = new KeyValuePair<string, string>("class", "btn dropdown-toggle");
            KeyValuePair<string, string> data_toggle = new KeyValuePair<string, string>("data-toggle", "dropdown");
            titleTag.Attributes.Add(@class);
            titleTag.Attributes.Add(data_toggle);
            TagBuilder span = new TagBuilder("span");
            span.Attributes.Add(new KeyValuePair<string, string>("class", "caret"));
            titleTag.SetInnerText(btnGroup.GroupTitle);
            titleTag.InnerHtml += span.ToString();
            TagBuilder ul = new TagBuilder("ul");
            ul.Attributes.Add(new KeyValuePair<string, string>("class", "dropdown-menu"));
            foreach (var value in btnGroup.ATagList)
            {
                var itemTag = new TagBuilder("li");
                var atag = new TagBuilder("a");
                atag.SetInnerText(value.LinkString);
                atag.Attributes.Add(new KeyValuePair<string, string>("href", "/" + value.ControllerName + "/" + value.ActionName));
                itemTag.InnerHtml += atag.ToString();
                ul.InnerHtml += itemTag.ToString();
            }
            divPanel.InnerHtml += titleTag.ToString() + ul.ToString();

            return new MvcHtmlString(divPanel.ToString());
        }
        public static MvcHtmlString BootstrapModalDialogBtn(this HtmlHelper htmlHelper, string ButtonString, string ModalId)
        {
            var aBtn = new TagBuilder("a");
            var modalId = ModalId;
            aBtn.Attributes.Add("href", "#" + modalId);
            aBtn.Attributes.Add("role", "button");
            aBtn.Attributes.Add("class", "btn");
            aBtn.Attributes.Add("data-toggle", "modal");
            aBtn.SetInnerText(ButtonString);
            return new MvcHtmlString(aBtn.ToString());
        }

        /// <summary>
        /// render a modal dialog based on bootstrap v2.3.2(http://getbootstrap.com/2.3.2/javascript.html#modals)
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="partialViewName"></param>
        /// <param name="model"></param>
        /// <param name="ButtonString"></param>
        /// <param name="ModalHeader"></param>
        /// <param name="CloseBtnText"></param>
        /// <param name="SaveChangesText"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString BootstrapModalDialog(this HtmlHelper htmlHelper, string partialViewName, object model, string ButtonString, string ModalHeader, string CloseBtnText, string SaveChangesText, object htmlAttributes)
        {
            var modalId = Guid.NewGuid().ToString();
            var aBtn = BootstrapModalDialogBtn(htmlHelper, ButtonString, modalId);
            var ModalBody = BootstrapModalDialogBody(htmlHelper, partialViewName, model, modalId, ModalHeader, CloseBtnText, SaveChangesText, htmlAttributes);

            return new MvcHtmlString(aBtn.ToString() + ModalBody.ToString());

        }

        public static MvcHtmlString BootstrapModalDialogBody(this HtmlHelper htmlHelper, string partialViewName, object model, string ModalId, string ModalHeader, string CloseBtnText, string SaveChangesText, object htmlAttributes)
        {
            var panelDiv = new TagBuilder("div");

            var modalMainDiv = new TagBuilder("div");
            modalMainDiv.Attributes.Add("id", ModalId);
            modalMainDiv.Attributes.Add("class", "modal hide fade");
            modalMainDiv.Attributes.Add("tabindex", "-1");
            modalMainDiv.Attributes.Add("role", "dialog");
            modalMainDiv.Attributes.Add("aria-labelledby", "myModalLabel" + ModalId);
            modalMainDiv.Attributes.Add("aria-hidden", "true");

            var headerDiv = new TagBuilder("div");
            headerDiv.Attributes.Add("class", "modal-header");

            var closeBtn = new TagBuilder("button");
            closeBtn.Attributes.Add("type", "button");
            closeBtn.Attributes.Add("class", "close");
            closeBtn.Attributes.Add("data-dismiss", "modal");
            closeBtn.Attributes.Add("aria-hidden", "true");
            closeBtn.SetInnerText("×");
            headerDiv.InnerHtml += closeBtn.ToString();

            var headerH3 = new TagBuilder("h3");
            headerH3.Attributes.Add("id", "myModalLabel" + ModalId);
            headerH3.SetInnerText(ModalHeader);
            headerDiv.InnerHtml += headerH3.ToString();

            var bodyDiv = new TagBuilder("div");
            bodyDiv.Attributes.Add("class", "modal-body");
            var bodyHrml = Html.PartialExtensions.Partial(htmlHelper, partialViewName, model);
            bodyDiv.InnerHtml += bodyHrml;

            var footerDiv = new TagBuilder("div");
            footerDiv.Attributes.Add("class", "modal-footer");

            var btn_close = new TagBuilder("button");
            btn_close.Attributes.Add("class", "btn");
            btn_close.Attributes.Add("data-dismiss", "modal");
            btn_close.Attributes.Add("aria-hidden", "true");
            btn_close.SetInnerText(CloseBtnText);

            var btn_save = new TagBuilder("button");
            btn_save.Attributes.Add("class", "btn btn-primary modal-submit");
            btn_save.SetInnerText(SaveChangesText);

            footerDiv.InnerHtml += btn_close;
            footerDiv.InnerHtml += btn_save;

            modalMainDiv.InnerHtml += headerDiv;
            modalMainDiv.InnerHtml += bodyDiv;
            modalMainDiv.InnerHtml += footerDiv;

            panelDiv.InnerHtml += modalMainDiv.ToString();
            return new MvcHtmlString(modalMainDiv.ToString());
        }
        public static MvcHtmlString BootstrapDatetimepicker(this HtmlHelper html)
        {
            return BootstrapDatetimepicker(html, DateTimeFormate, DateTime.Now,null);
        }
        public static MvcHtmlString BootstrapDatetimepicker(this HtmlHelper html, DateTime dtStart)
        {
            return BootstrapDatetimepicker(html, DateTimeFormate, dtStart,null);
        }
        public static MvcHtmlString BootstrapDatetimepicker(this HtmlHelper html, string dtFormate, DateTime dtStart, object htmlAttributes)
        {
            //<div data-link-field="dtp_input1" data-date-format="dd MM yyyy - HH:ii p" data-date="1979-09-16T05:25:07Z" class="controls input-append date form_datetime">
            //        <input type="text" readonly="" value="" size="16"/>
            //        <span class="add-on"><i class="icon-remove"></i></span>
            //        <span class="add-on"><i class="icon-th"></i></span>
            //<input id="dtp_input1" type="hidden" value="1979-09-06 08:05:07">
            // </div>
            var wrapperDiv = new TagBuilder("div");
            var selectorId = Guid.NewGuid().ToString();
            var startdtstring = dtStart.ToString(dtFormate);
            TagBuilder mainDiv = new TagBuilder("div");
            mainDiv.Attributes.Add("data-link-field", selectorId);
            mainDiv.Attributes.Add("data-date-format", dtFormate);
            mainDiv.Attributes.Add("data-date", startdtstring);
            mainDiv.Attributes.Add("class", "controls input-append date form_datetime");

            TagBuilder inputBox = new TagBuilder("input");
            inputBox.Attributes.Add("type", "text");
            inputBox.Attributes.Add("readonly", "");
            inputBox.Attributes.Add("value", "");
            inputBox.Attributes.Add("size", "16");

            var htmlDic = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
            inputBox.MergeAttributes(htmlDic, false);

            mainDiv.InsertTag(inputBox);
            TagBuilder span1 = new TagBuilder("span");
            span1.AddCssClass("add-on");

            TagBuilder i1 = new TagBuilder("i");
            i1.AddCssClass("icon-remove");
            span1.InsertTag(i1);

            TagBuilder span2 = new TagBuilder("span");
            span2.AddCssClass("add-on");

            TagBuilder i2 = new TagBuilder("i");
            i2.AddCssClass("icon-th");
            span2.InsertTag(i2);

            TagBuilder hiddenSelectorBody = new TagBuilder("input");
            hiddenSelectorBody.Attributes.Add("id", selectorId);
            hiddenSelectorBody.Attributes.Add("type", "hidden");
            hiddenSelectorBody.Attributes.Add("data-date", startdtstring);

            mainDiv.InsertTag(span1);
            mainDiv.InsertTag(span2);
            wrapperDiv.InsertTag(mainDiv);
            wrapperDiv.InsertTag(hiddenSelectorBody);
            return new MvcHtmlString(wrapperDiv.ToString());

        }

        public static MvcHtmlString BootstrapDatetimepickerFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression)
        {
            return BootstrapDatetimepickerFor<TModel, TProperty>(html, expression, DateTimeFormate, DateTime.Now);
        }

        public static MvcHtmlString BootstrapDatetimepickerFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, string dtFormate, DateTime dtStart)
        {
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, html.ViewData);
            var mx = metadata.Model;
            var exstring = ExpressionHelper.GetExpressionText(expression);
            var inputTag = new { name = metadata.PropertyName };
            return BootstrapDatetimepicker(html, dtFormate, dtStart, inputTag);
        }
        public static TagBuilder InsertTag(this TagBuilder parents, TagBuilder children)
        {
            parents.InnerHtml += children;
            return parents;
        }
    }
}

